@extends('admin.layout.layout')
@section('title','帖子列表')
@section('con')
<div id="page-wrapper" style="min-height: 349px;">
            <div class="row">
                <div class="col-lg-12">
                    <h1 class="page-header">帖子列表</h1>
                </div>
                <!-- /.col-lg-12 -->
            </div> 
            <!-- /.row -->
            <div class="row">
                <div class="col-lg-12">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            帖子列表
                        </div>
                        <!-- /.panel-heading -->
                        <div class="panel-body">
                            <div class="dataTable_wrapper">
                                <div id="dataTables-example_wrapper" class="dataTables_wrapper form-inline dt-bootstrap no-footer">
                                <div class="row">
                                <form action="{{url('/admin/post/index')}}" method="get" >
	                                <div class="col-sm-6">
                                        <div class="dataTables_length" id="dataTables-example_length">
                                            <label>选择分区、板块
                                                <select name="sid" aria-controls="dataTables-example" class="form-control input-sm block_sid">
                                                         <option value="0" >所有分区</option>                                   
                                                         <option value="1" @if(!empty($list['sid']) && $list['sid'] == 1) { selected } @endif>NBA区</option>                                   
                                                         <option value="2" @if(!empty($list['sid']) && $list['sid'] == 2) { selected } @endif>交易区</option>                                   
                                                         <option value="3" @if(!empty($list['sid']) && $list['sid'] == 3) { selected } @endif>技术区</option>                                   
                                                </select> 
                                                <select name="bid" aria-controls="dataTables-example" class="form-control input-sm block_bid" style="display:none;">
                                                        <option value="">所有板块</option>
                                                </select> 
                                            </label>
                                        </div>
                                    </div>
                                    <div class="col-sm-6">
                                        <div id="dataTables-example_filter" class="dataTables_filter">
                                            <label><input name="keywords" value="{{ $list['keywords'] or '' }}" type="search" class="form-control input-sm" placeholder="输入作者或标题" aria-controls="dataTables-example" /></label>
                                            <button class="btn btn-primary "  title="点击查询内容" >查询</button>
                                        </div>          
	                                </div>
                                   <input type="hidden" name="num" value="10">
                                </form>
                                <div class="row">
                                	<div class="col-sm-12">
                                		<table class="table table-striped table-bordered table-hover dataTable no-footer" id="dataTables-example" role="grid" aria-describedby="dataTables-example_info">
                                    		<thead>
                                    			<tr>
		                                    		<th>帖子Id号</th>
                                                    <th>所属分区</th>
                                                    <th>所属板块</th>
                                                    <th>作者</th>
		                                    		<th>帖子标题<span style="color:#9999A6;font-size:12px;">(点击标题查看内容)</span></th>
                                                    <th>发帖时间</th>
                                                    <th>阅览量</th>
		                                    		<th>回复数</th>
                                                    <th>帖子状态</th>
		                                    		
		                                    		<th>操作</th>
                                    			</tr>
                                    		</thead>
                                    	<tbody>
                                    	@foreach($p as $k=>$v)
		                                    <tr class="gradeA odd" role="row">
		                                        <td class="sorting_1">{{$v->pid}}</td>
                                                <td class="sorting_2">{{$v->sname}}</td>
		                                        <td class="sorting_2">{{$v->bname}}</td>
		                                        <td class="sorting_3">{{$v->username}}</td>
                                                <td class="center"><a href="" class="showPost" title="点击查看帖子详情" data-toggle="modal" data-target="#myModal_{{$v->pid}}">{{$v->pname}}</a></td>
		                                        <td class="center">{{$v->pctime}}</td>
                                                <td class="center">{{$v->ctr}}</td>
                                                <td class="center">{{$v->num}}</td>
                                                <td class="center edit_pstaus end_<?php echo $v->pid;  ?> " title="双击设置状态"><?php $arr = ['锁定','普通','置顶','锁定和置顶']; echo $arr[ $v->pstaus ]; ?>
                                                    
                                                </td>
		                                        <td class="center">
		                                        	<button  class="btn btn-danger btn-sm delete-btn" type="button" title="删除帖子">
                              						  删除
                           							</button>
		                                        </td>
		                                    </tr>
                                                <!-- Modal -->
                                                <div class="modal fade" id="myModal_{{$v->pid}}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                                                  <div class="modal-dialog">
                                                    <div class="modal-content">
                                                      <div class="modal-header">
                                                        <button type="button" onclick="$(this).parents('div').find('.ShowFollowop').empty();" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                                                        <h4 class="modal-title" id="myModalLabel">{{$v->pname}}</h4>
                                                      </div>
                                                      <div class="modal-body">
                                                        {!! $v->pcont !!} 
                                                      </div>
                                                      <div class="modal-footer ShowFollowop">
                                                        
                                                      </div>
                                                      <div class="modal-footer">
                                                        <button type="button" onclick="$(this).parents('div').find('.ShowFollowop').empty();" class="btn btn-default" data-dismiss="modal">关闭</button>
                                                      </div>
                                                    </div>
                                                  </div>
                                                </div>



		                                @endforeach 
                                    	</tbody>
                                </table>
                                </div>
                                </div>
                                <div class="row" style="margin:0px auto">
                                  
                                        
                                       {!! $p->appends($list)->render() !!}        
                                    
                                </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
@endsection
@section('js')
<script type="text/javascript">     
    $.ajaxSetup({
        headers:{
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });


    //绑定单击事件
    $('.delete-btn').click(function(){
        if (confirm('确定要删除吗?')) {
             var id = $(this).parents('tr').find('.sorting_1').html();
             var btn = $(this);

             //发送ajax请求
             $.post('/admin/post/delete',{pid:id},function(data){
              if (data == 0) {
                //删除成功
                alert('删除成功');
                btn.parents('tr').remove();
                }else{
                //删除失败
                alert('删除失败');
                }
            })
        }
        })

    //绑定事件
    $('.block_sid').change(function()
    {
        //获取值
        var sid = $(this).val();
        //判断下拉内容值
        if (sid == 0) {$('.block_bid').css('display','none');return false;};
        $('.block_bid').css('display','inline-block');
        var str = '<option value="">所有板块</option>';

        $.get('/admin/post/select',{sid,sid},function(data){
            eval('var obj ='+ data);
            for (var i = 0; i <  obj.length; i++) {
                str += '<option value="'+obj[i].bid+'">'+obj[i].bname+'</option>';
            };
                $('.block_bid').html(str);
            })
    })

    //绑定双击事件
    $('.edit_pstaus').dblclick(function(){
        $(this).empty();
        var pstaus = $(this);
        var pid = $(this).parents('tr').find('.sorting_1').html();
        var str = '';

        $.get('/admin/post/pstaus',{pid,pid},function(data){
            eval('var obj ='+ data);
            var arr = ['锁定','普通','置顶','锁定和置顶'];
            str = '<select name="pstaus" id=""><option value="'+obj[0].pstaus+'">'+arr[obj[0].pstaus]+'</option>';
            for (var i = 0; i <  arr.length; i++) {
                 str += '<option class="'+i+'"  value="'+i+'">'+arr[i]+'</option>';
            };
            str += '</select>';
            pstaus.html(str);
        })

    });

 //绑定window双击事件
    $(window).dblclick(function(){
        var num = $('.edit_pstaus select');
        var pid = num.parents('tr').find('.sorting_1').html();
        var pstaus = num.val();
        var arr = ['锁定','普通','置顶','锁定和置顶'];
        if (pid != null) {
            $.get('/admin/post/update',{pid:pid,pstaus:pstaus},function(data){
                if (data == 1) {
                    //修改成功
                    str = $('.edit_pstaus select').val();
                    $('.edit_pstaus select').remove();
                    $('.end_'+pid).html( arr[str] );
                    alert('修改成功');
                }else{
                    //修改失败
                   
                    str = $('.edit_pstaus select').val();
                    $('.edit_pstaus select').remove();
                    $('.end_'+pid).html( arr[str] );
                }   
            });
        };
        // $('.edit_pstaus').empty();

        // console.log($('.edit_pstaus select').val());
        // console.log($('.edit_pstaus').val());
        // console.log($('.end_'+pid));
    }); 

     //绑定单击事件
    $('.showPost').click(function(){
        //获取nid
        var pid = $(this).parents('tr').find('.sorting_1').html();
        //发送ajax
        $.get('/admin/post/followup',{fpid:pid},function(data){
            if (data != null) {
                eval('var obj ='+ data);
                var div = $('.ShowFollowop');
                var str = '';
                for (var i = 0; i <  obj.length; i++) {
                    str += '<div class=""><p>'+obj[i].fcont+'</p><h5>评论者:'+obj[i].username+'</h5>&nbsp;&nbsp;&nbsp;'+obj[i].fctime+'&nbsp;'+(i+1)+'楼</div><hr>' ;
                      div.html(str);      
                };
            };
        });
    });

</script>
@endsection